import matplotlib.pyplot as plt
import seaborn as sns
import math
import numpy as np

def cdf(fn, div='init', filter=''):
    x = []
    found = False
    for line in open(fn):
        if found:
            if '=' in line:
                break
            arr = line.split(',')
            if filter in line:
                x.append(float(arr[1]))
        if found == False and div in line:
            found = True
            
    print('#updates: %s' % len(x))
    if (len(x) == 0):
        return
    y = [d - x[0] for d in x]

    sns.ecdfplot(data=y)
    plt.show()

def scatter(fn, div):
    x = []
    y = []
    found = False
    for line in open(fn):
        if found:
            if '=' in line:
                break
            arr = line.split(',')
            x.append(float(arr[1]))
            y.append(arr[0])
        if found == False and div in line:
            found = True

    print('#updates: %s' % len(x))
    if (len(x) == 0):
        return

    x1 = [d - x[0] for d in x]

    plt.scatter(x1, y, s=3)
    plt.xlabel('time(s)')
    plt.show()

def hist(fn, div, b=1):
    x = []
    found = False
    for line in open(fn):
        if found:
            if '=' in line:
                break
            arr = line.split(',')
            x.append(float(arr[1]))
        if found == False and div in line:
            found = True
            
    print('#updates: %s' % len(x))
    if (len(x) == 0):
        return
    y = [d - x[0] for d in x]

    y,binEdges=np.histogram(y,bins=math.ceil((y[-1])) * b)
    bincenters = 0.5*(binEdges[1:]+binEdges[:-1])
    plt.plot(bincenters,y,'-')
    # plt.hist(y, math.ceil(y[-1]), histtype='step')
    plt.show()
cdf('bgp-down.log', 'down', '11.0.0.1')
# scatter('init-k8.log')
# hist('log', 'up',1)